package com.power.sifa.account.dao.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.plf.core.base.model.Ha_depart;
import com.plf.core.base.model.Ha_user;
import com.plf.core.common.dao.OffersetTransformers;
import com.power.sifa.account.dao.Ha_user_approveDao;
import com.power.sifa.account.model.pageInfo.Ha_user_approvePageInfo;
import com.power.sifa.correction.model.Cre_warning;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.query.internal.NativeQueryImpl;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Repository
public class Ha_user_approveDaoImpl implements Ha_user_approveDao {

    private final EntityManager entityManager;

    public Ha_user_approveDaoImpl(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public List<Ha_depart> queryDepartList(String departid, Ha_user_approvePageInfo pageInfo) {
        List<Ha_depart> result;
        StringBuffer sql = new StringBuffer("select * from ha_depart where 1=1 ");
        List<Object> paramsList = new ArrayList<>();
        int index = 1;

//        sql.append(" and parentid = ?" + index++);
//        paramsList.add(departid);

        // 节点类型查询
        if (null != pageInfo.getType()) {
            sql.append(" and type = ?" + index++);
            paramsList.add(pageInfo.getType());
        }
        // 排序
        sql.append(" order by sortnum asc");
        Query query = entityManager.createNativeQuery(sql.toString());
        query.unwrap(NativeQueryImpl.class).setResultTransformer(OffersetTransformers.ALIAS_TO_ENTITY_MAP);
        log.info(sql.toString());

        // 添加条件
        for (int i = 0; i <= paramsList.size() - 1; i++) {
            query.setParameter(i + 1, paramsList.get(i));
        }
        List<Map> list = query.getResultList();
        result = JSON.parseArray(JSON.toJSONString(list), Ha_depart.class);
        return result;
    }

    @Override
    public List<Map> queryUserByDepartids(List<String> departIds, Ha_user_approvePageInfo pageInfo) {

        StringBuffer sql = new StringBuffer("select b.*, a.departid, c.sectionid from ha_depart_user a left join ha_user b on a.userid = b.userid left join ha_depart c on a.departid = c.id where 1=1 ");
        List<Object> paramsList = new ArrayList<>();
        int index = 1;
        sql.append(" and + " + pageInfo.getJoinfield() + " in ?" + index++);
        paramsList.add(departIds);

        Query query = entityManager.createNativeQuery(sql.toString());
        query.unwrap(NativeQueryImpl.class).setResultTransformer(OffersetTransformers.ALIAS_TO_ENTITY_MAP);
        log.info(sql.toString());

        // 添加条件
        for (int i = 0; i <= paramsList.size() - 1; i++) {
            query.setParameter(i + 1, paramsList.get(i));
        }
        return query.getResultList();
    }
}
